package com.yantong.controller.platform;

import com.yantong.annotation.Log;
import com.yantong.controller.BaseController;
import com.yantong.exception.BDException;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.PayChannel;
import com.yantong.service.PayChannelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 支付通道管理
 * @author kanghaixin
 * @date 2018/10/21.16:19
 */
@Controller
@RequestMapping("/mall/payChannel")
public class PayChannelController extends BaseController {

    private static final String prefix = "mall/payChannel";

    @Autowired
    private PayChannelService payChannelService;
    /**
     * 支付通道列表界面
     * @return
     */
    @GetMapping()
    @RequiresPermissions("mall:payChannel:payChannel")
    public String channels(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/channels";
    }

    /**
     * 支付通道列表数据
     * @param name
     * @param offset
     * @param limit
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("mall:payChannel:payChannel")
    public PageUtils<PayChannel> listChannels(Long centerId,String name, int offset, int limit) {
        if(null == centerId){
            centerId = getUserCentreId();
        }
        return payChannelService.listChannels(centerId,name, offset, limit);
    }

    /**
     * 支付通道新增页面
     * @return
     */
    @RequiresPermissions("mall:payChannel:add")
    @GetMapping("/add")
    public String add(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/add";
    }

    /**
     * 支付通道新增操作
     * @param payChannel
     * @return
     */
    @Log(module = "支付通道管理", operation = "新增支付通道")
    @ResponseBody
    @RequiresPermissions("mall:payChannel:add")
    @PostMapping("/save")
    public ResultVo save(PayChannel payChannel) {
        if (payChannel == null) {
            return ResultVo.error();
        }
        if(payChannel.getCenterId() == null){
            payChannel.setCenterId(getUserCentreId());
        }
        if(payChannel.getCenterId() == null){
            throw new BDException("请选择商户再操作");
        }
        payChannel.setOperName(getNikeName());
        payChannel.setOperId(getUserId());
        if (payChannelService.savePayChannel(payChannel) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 支付通道编辑页面
     * @param channelId
     * @param model
     * @return
     */
    @RequiresPermissions("mall:payChannel:edit")
    @GetMapping("/edit/{channelId}")
    public String edit(@PathVariable Long channelId, Model model) {
        model.addAttribute("channel", payChannelService.getByKey(channelId));
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/edit";
    }

    /**
     * 更新支付通道操作
     * @param payChannel
     * @return
     */
    @Log(module = "支付通道管理", operation = "编辑支付通道")
    @ResponseBody
    @RequiresPermissions("mall:payChannel:edit")
    @PostMapping("/update")
    public ResultVo updatePayChannel(PayChannel payChannel) {
        if (payChannel == null || payChannel.getId() == null) {
            return ResultVo.error();
        }
        payChannel.setOperId(getUserId());
        payChannel.setOperName(getNikeName());
        if (payChannelService.updatePayChannel(payChannel) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 删除支付通道记录
     * @param channelId
     * @return
     */
    @Log(module = "支付通道管理", operation = "删除支付通道")
    @ResponseBody
    @RequiresPermissions("mall:payChannel:remove")
    @PostMapping("/remove")
    public ResultVo remove(Long channelId) {
        if (channelId == null) {
            return ResultVo.error();
        }
        PayChannel pc = new PayChannel();
        pc.setOperId(getUserId());
        pc.setOperName(getNikeName());
        pc.setDel(1);
        pc.setId(channelId);
        if (payChannelService.removePayChannel(pc)< 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 不显示操作
     *
     * @param channelId
     * @return
     */
    @RequiresPermissions("mall:notice:showControl")
    @ResponseBody
    @PostMapping("/disableShow")
    public ResultVo disableShow(Long channelId) {
        if (channelId == null) {
            return ResultVo.error();
        }
        if (payChannelService.disableShow(channelId, getUserId(), getNikeName()) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 显示操作
     *
     * @param channelId
     * @return
     */
    @RequiresPermissions("mall:notice:showControl")
    @ResponseBody
    @PostMapping("/enableShow")
    public ResultVo enableShow(Long channelId) {
        if (channelId == null) {
            return ResultVo.error();
        }
        if (payChannelService.enableShow(channelId, getUserId(), getNikeName()) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 启用支付通道操作
     *
     * @param channelId
     * @return
     */
    @RequiresPermissions("mall:notice:showControl")
    @ResponseBody
    @PostMapping("/enableUse")
    public ResultVo enableUse(Long channelId) {
        if (channelId == null) {
            return ResultVo.error();
        }
        Long centerId = getUserCentreId();
        if(null == centerId){
            throw new BDException("无编辑权限");
        }
        if (payChannelService.enableUse(centerId,channelId, getUserId(), getNikeName()) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 启用支付通道操作
     *
     * @return
     */
    @RequiresPermissions("mall:payChannel:copy")
    @ResponseBody
    @PostMapping("/copyFromPlatform")
    public ResultVo copyFromPlatform() {
        Long centerId = getUserCentreId();
        if(null == centerId){
            throw new BDException("无操作权限");
        }

        payChannelService.copyFromPlatform(centerId);
        return ResultVo.ok();
    }
}
